package com.example.test.conview.view.core;

import static com.puty.conputyutils.DateUtils.GetDate_choice;
import static com.puty.conputyview.common.PutyConst.DATE;
import static com.puty.conputyview.common.PutyConst.HOUSEHOLD_SERIES;
import static com.puty.conputyview.common.PutyConst.INDUSTRY_SERIES;
import static com.puty.conputyview.common.PutyConst.SERIES_TYPE;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.puty.conputyutils.LogUtils;
import com.puty.conputyutils.MultiLanguageUtils;
import com.puty.conputyutils.SharedPreferencesHelperUtils;
import com.puty.conputyutils.StringUtils;
import com.puty.conputyview.R;
import com.puty.conputyview.common.PutyConst;
import com.puty.conputyview.view.DrawArea;
import com.puty.conputyview.view.tool.Global;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class TimeElement extends Element {

    private final static String TAG = "TimeElement";
    // 默认字体颜色
    public int fontColor = Color.BLACK;
    //待画内容列表
    String[] drawTextList;

    /**
     * 行间距 0=1.0 单倍行距 ,1=1.2倍行距,2=1.5倍行距,3=两倍行距,4=自定义
     */
    public int textRowMode = 0;

    /**
     * 行间距
     */
    public int textRowSpace = 0;

    /**
     * 自动换行 1 换行 0 不换行
     */
    public int autoBreak = 1;
    public Bitmap tempImg;
    public Bitmap img;

    public String[] date_choiceTW = {mContext.getString(R.string.no2), "yyyy MM dd ", "yyyy MM ", "MM dd ", "yyyy-MM-dd ", "yyyy-MM ", "MM-dd ", "yyyy/MM/dd ", "yyyy/MM ", "MM/dd "};
    public String[] time_choice = {mContext.getString(R.string.no2), "HH:mm:ss", "HH:mm", "mm:ss"};
    public String[] time_choiceTW = {mContext.getString(R.string.no2), "HH:mm:ss ", "HH:mm  ", "mm:ss  "};
    public String time;//当前时间
    public String automaticDate;//当前显示的年月日
    public String automaticTime;//当前显示的时分秒
    private TextView tvElementSketchpad;

    public TimeElement(Context context, String content, float width, float height, Label lb) {
        super(context, lb);
        if (TextUtils.isEmpty(content)) {
            time_format = 0;
            date_format = 7;
            // 包含日年日时分秒
            String pattern;
            if (MultiLanguageUtils.isTaiwan(context)) {
                pattern = date_choiceTW[date_format];
            } else {
                pattern = GetDate_choice(mContext)[date_format];
            }
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            datedeviation = format.format(new Date());
            timedeviation = "";
            super.content = datedeviation.trim(); //+ transformTime.format(curDate);
        } else {
            super.content = content;
        }
        super.width = width;
        super.height = height;
        super.type = DATE;
        if (SERIES_TYPE == INDUSTRY_SERIES) {
            this.fontIndex = 20;
            this.fontIndex = getFontIndex(2, lb.Width, lb.Height);
            //字体大小
            this.fontSize = Global.fontSize[Math.abs(fontIndex)] * 8 * lb.scale;
        }
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            super.orientation = lb.printInfo.PrintDirect;
            if (orientation == 0) {
                super.width = (content.length() + 0.5f) * Global.fontSize[fontIndex] * 8 * lb.scale;
            } else {
                super.height = (content.length() + 0.5f) * Global.fontSize[fontIndex] * 8 * lb.scale;
            }
            setFontSize();
            View elementTextYY = LayoutInflater.from(context).inflate(R.layout.element_text_yy, null);
            tvElementSketchpad = elementTextYY.findViewById(R.id.tv_text);
        }
    }

    /*
     * 设置字体大小
     * */
    @Override
    public void setFontSize() {
        super.setFontSize();
        //字体大小
        float fz = Global.fontSize[Math.abs(fontIndex)] * 8 * lb.scale;
        if (DrawArea.dragView != null) {
            // 微调字体大小。
            // 生成打印图片时等比例缩放字体，字体大小会丢失，所以在这里处理，保证压缩字体时，字体大小是整数
            float fontRadio = 1.0f;
            float labelHeight = lb.labelHeight;
            int theme = (int) SharedPreferencesHelperUtils.getSharedPreference("theme", R
                    .style.AppTheme);
            if (theme == R.style.YYTheme) {
                if (labelHeight < 9) {
                    fontRadio = supplement8Number((int) (6.0f * 7.09f)) / (9.f * 8.f * lb.scale);
                } else if (labelHeight == 9) {
                    fontRadio = supplement8Number((int) (9.0f * 7.09f)) / (9.f * 8.f * lb.scale);
                } else {
                    fontRadio = supplement8Number((int) (9.0f * 7.09f)) / (7.f * 8.f * lb.scale);
                }
            } else { // YY
                fontRadio = supplement8Number((int) (9.0f * 7.09f)) / (9.f * 8.f * lb.scale);
            }

            double fontSize = Math.floor(fz * fontRadio);
            fz = (float) (fontSize / fontRadio);
        }

        this.fontSize = fz;
    }

    /**
     * 宽度不是8的倍数，需要转成8的倍数，否则生成黑白图片的时候，会舍弃掉一部分
     *
     * @return
     */
    private float supplement8Number(int number) {
        if (number % 8 != 0) {
            return (8 - number % 8) + number + 0.0f;
        } else {
            return number + 0.0f;
        }
    }

    @Override
    public void init() {
        super.init();
        if (TextUtils.isEmpty(this.content)) {
            this.content = mContext.getString(R.string.text);
        }
        initBitmap();
        this.img = this.tempImg;
        rate(rate);
    }

    /**
     * 生成图片
     */
    private void initBitmap() {
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        Date curDate = new Date();
        SimpleDateFormat transformDate = null;
        SimpleDateFormat transformTime = null;
        if (!MultiLanguageUtils.isTaiwan(mContext)) {
            // 防止下标越界
            if (date_format > 0 && date_format < GetDate_choice(mContext).length) {
                transformDate = new SimpleDateFormat(GetDate_choice(mContext)[date_format]);
            }
            // 防止下标越界
            if (time_format > 0 && time_format < GetDate_choice(mContext).length) {
                transformTime = new SimpleDateFormat(time_choice[time_format]);
            }
        } else {
            if (SERIES_TYPE == HOUSEHOLD_SERIES) {
                // 防止下标越界
                if (date_format > 0 && date_format < GetDate_choice(mContext).length) {
                    transformDate = new SimpleDateFormat(GetDate_choice(mContext)[date_format]);
                }
                // 防止下标越界
                if (time_format > 0 && time_format < GetDate_choice(mContext).length) {
                    transformTime = new SimpleDateFormat(GetDate_choice(mContext)[time_format]);
                }
            } else if (SERIES_TYPE == INDUSTRY_SERIES) {
                // 防止下标越界
                if (date_format > 0 && date_format < GetDate_choice(mContext).length) {
                    transformDate = new SimpleDateFormat(date_choiceTW[date_format]);
                }
                // 防止下标越界
                if (time_format > 0 && time_format < GetDate_choice(mContext).length) {
                    transformTime = new SimpleDateFormat(time_choiceTW[time_format]);
                }
            }


        }
        if ((!TextUtils.isEmpty(datedeviation) && datedeviation.length() > 3) && (!TextUtils
                .isEmpty(timedeviation) && timedeviation.length() > 3)) {
            content = datedeviation + timedeviation;
        } else if (!TextUtils.isEmpty(datedeviation) && datedeviation.length() > 3) {
            content = datedeviation.trim();
        } else if (!TextUtils.isEmpty(timedeviation) && timedeviation.length() > 3) {
            content = timedeviation;
        } else {
            String formatData = transformDate == null ? "" : transformDate.format(curDate);
            String formatTime = transformTime == null ? "" : transformTime.format(curDate);
            automaticDate = TextUtils.isEmpty(formatData) ? "" : formatData;
            automaticTime = TextUtils.isEmpty(formatTime) ? "" : formatTime;
            content = automaticDate + automaticTime;
        }

        //字体
        if (typeface != null) {
            textPaint.setTypeface(typeface);
        }
        //设置字体大小
        textPaint.setTextSize(this.fontSize);

        textPaint.setFakeBoldText(fontBlod == 0 ? false : true);
        textPaint.setTextSkewX(fontItalic == 0 ? 0 : -0.25f);
        textPaint.setUnderlineText(fontUnderline == 0 ? false : true);
        textPaint.setStrikeThruText(fontDelete == 0 ? false : true);

        textPaint.setColor(fontColor);
        textPaint.setStrokeCap(Paint.Cap.ROUND);
        textPaint.setStrokeJoin(Paint.Join.ROUND);
        textPaint.setLinearText(true);

        if (this.rate == 0 || this.rate == 180) {
            this.height = getTextReelHeight(textPaint);//预估文本的实际高度
        } else {
            this.width = getTextReelHeight(textPaint);//预估文本的实际高度
        }
        Bitmap bmp;
        float width = this.width;
        float height = this.height;
        if (this.rate == 90 || this.rate == 270) {
            float temp = 0;
            temp = width;
            width = height;
            height = temp;
        }
        if (width <= 0) {
            width = 1;
        }
        if (height <= 0) {
            height = 1;
        }
        bmp = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bmp);


        //获取字体高度
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float textHeight = fontMetrics.descent - fontMetrics.ascent;

        float left = 0;
        float top = 0;
        String[] arraysStrings = this.drawTextList; //_content.split("\n");
        for (int k = 0; k < arraysStrings.length; k++) {
            //换行
            if (this.autoBreak == 1) {
                if (k > 0) {
                    top += textHeight;//换行
                    top += this.textRowSpace;//加上行距
                }
            }
            switch (textMode) {
                case 1:
                    textalignCenter(left, top, textHeight, arraysStrings[k], textPaint, canvas);
                    break;
                case 2:
                    textalignRight(left, top, textHeight, arraysStrings[k], textPaint, canvas);
                    break;
                case 3://拉伸
                    int padding = this.fontItalic == 1 ? 8 : 0;
                    float space = 0;
                    int index = k;
                    if (arraysStrings.length > 1 && k == arraysStrings.length - 1) {
                        index = arraysStrings.length - 2;
                    }
                    int len = arraysStrings[index].length();
                    //获取总的长度
                    float total = getContentWhidth(textPaint, arraysStrings[index]);

                    float surplusWidth = width - total - textCellSpace * (len - 1) - padding;
                    space = surplusWidth / (len - 1);//获取字符空隙 总宽度-字符串宽度
                    textalignPP(left - (this.fontItalic == 1 ? 4 : 0), top, textHeight,
                            arraysStrings[k], textPaint, canvas, space);
                    break;
                default:
                    textalignLeft(left, top, textHeight, arraysStrings[k], textPaint, canvas);
                    break;
            }

            this.tempImg = bmp;
        }
    }

    @Override
    public void draw(Canvas canvas) {
        if (width == 0 || height == 0) return;
        Canvas textCanvas = new Canvas();
        Bitmap textBitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
        textCanvas.setBitmap(textBitmap);
        //textCanvas.drawColor(Color.parseColor("#ffffffff"));
        // 多行处理
        String[] lines = content.split("\r\n|\r|\n", -1);

        if (orientation == 0) {
            float childHeight = height / lines.length;
            tvElementSketchpad.getPaint().setTextAlign(Paint.Align.LEFT);
            for (int i = 0; i < lines.length; i++) {
                textCanvas.drawText(lines[i], 0, (float) (((i + 0.5) * childHeight) - (tvElementSketchpad.getPaint().getFontMetrics().ascent + tvElementSketchpad.getPaint().getFontMetrics().descent) / 2), tvElementSketchpad.getPaint());
            }
        } else {
            tvElementSketchpad.getPaint().setTextAlign(Paint.Align.CENTER);

            // 1、计算每行宽度
            ArrayList<Float> lineWidthList = new ArrayList<>();
            float totalWidth = 0;
            for (int i = 0; i < lines.length; i++) {
                String item = lines[i];
                float maxLineWidth = 0;
                for (int k = 0; k < item.length(); k++) {
                    String s = String.valueOf(item.charAt(k));
                    float w = tvElementSketchpad.getPaint().measureText(s);
                    if (maxLineWidth < w) {
                        maxLineWidth = w;
                    }
                }
                if (maxLineWidth == 0) {
                    if (TextUtils.isEmpty(content)) {
                        maxLineWidth = tvElementSketchpad.getPaint().measureText("一");
                    } else {
                        maxLineWidth = tvElementSketchpad.getPaint().measureText(String.valueOf(content.charAt(0)));
                    }
                }
                totalWidth += maxLineWidth;
                lineWidthList.add(maxLineWidth);
            }

            // 2、绘制
            float x = 0;
            float childHeight = tvElementSketchpad.getPaint().descent() - tvElementSketchpad.getPaint().ascent();
            for (int i = 0; i < lines.length; i++) {
                if (i == 0) { // 从右往左排
                    x = (width + totalWidth) / 2 - lineWidthList.get(i) / 2;
                } else {
                    x -= (lineWidthList.get(i) + lineWidthList.get(i - 1)) / 2;
                }

                String item = lines[i];
                char[] indexs = getKeyChar(item);
                int textCount = item.toCharArray().length;

                if (textCount > 0) {

                    Paint.FontMetrics fm = tvElementSketchpad.getPaint().getFontMetrics();

                    for (int k = 0; k < textCount; k++) {
                        textCanvas.drawText(
                                String.valueOf(indexs[k]), x
                                , (float) (((k + 0.5) * childHeight) - (fm.ascent + fm.descent) / 2)
                                ,
                                tvElementSketchpad.getPaint());
                    }
                }
            }
        }
        this.img = textBitmap;
        //原始区域
        Rect sr = new Rect(0, 0, this.img.getWidth(), this.img.getHeight());
        canvas.drawBitmap(this.img, sr, new RectF(left, top, left + this.img.getWidth(), top + this.img.getHeight()), null);
        super.draw(canvas);
        super.draw(canvas);
    }


    /**
     * 获取时间
     *
     * @return Date
     */
    public Date getDate() {
        String pattern = "";
        if (!MultiLanguageUtils.isTaiwan(mContext)) {
            if (date_format > 0 && time_format > 0) {
                pattern = GetDate_choice(mContext)[date_format] + time_choice[time_format];
            } else if (date_format > 0) {
                pattern = GetDate_choice(mContext)[date_format];
            } else if (time_format > 0) {
                pattern = time_choice[time_format];
            }
        } else {
            if (date_format > 0 && time_format > 0) {
                pattern = date_choiceTW[date_format] + time_choiceTW[time_format];
            } else if (date_format > 0) {
                pattern = date_choiceTW[date_format];
            } else if (time_format > 0) {
                pattern = time_choiceTW[time_format];
            }
        }
        Date date = null;
        LogUtils.i(TAG, "pattern : " + pattern);
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        ParsePosition pos = new ParsePosition(0);
        LogUtils.i(TAG, "mContext : " + mContext);
        //String str = _content.replaceAll("/", "-");
        date = formatter.parse(content, pos);
        LogUtils.i(TAG, "===> date : " + date);
        return date;
    }

    /**
     * 预估文本高度
     */
    private float getTextReelHeight(Paint textPaint) {
        float w = this.width;
        float h = this.height;
        if (this.rate == PutyConst.RATE_90 || this.rate == PutyConst.RATE_270) {
            float temp = w;
            w = h;
            h = temp;
        }
        //获取字体高度
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float textHeight = fontMetrics.descent - fontMetrics.ascent;

        float left = 0;
        float top = 0;
        String result = "";
        String[] arraysStrings = content.split("\n");
        for (int k = 0; k < arraysStrings.length; k++) {
            //换行
            if (this.autoBreak == 1) {
                if (k > 0 && !TextUtils.isEmpty(result)) {
                    result += "\n";
                    top += textHeight;//换行
                    top += this.textRowSpace;//加上行距
                }
            }
            char[] chars = arraysStrings[k].toCharArray();
            if (isArab || StringUtils.textContainsKhmer(content)) {
                result += chars[0];
                int start = 0;
                for (int i = 1; i < arraysStrings[k].length(); i++) {
                    String vString = arraysStrings[k].substring(start, i);
                    //单个字体宽度
                    float actulWidth = getContentWhidth(textPaint, vString);//计算字符串宽度
                    float singleCharacterWidth = getContentWhidth(textPaint, "ش");//计算最宽的字符的宽度
                    if (actulWidth > w - singleCharacterWidth && !TextUtils.isEmpty(result)) {//去掉一个字符长度
                        top += textHeight;//换行
                        top += this.textRowSpace;//加上行距
                        result += "\n";
                        start = i;
                    }
                    result += chars[i];
                }
            } else {
                char[] arr = arraysStrings[k].toCharArray();
                int len = arr.length;
                for (int i = 0; i < len; i++) {
                    String vString = String.valueOf(arr[i]);
                    //单个字体宽度
                    float actulWidth = getContentWhidth(textPaint, vString);//计算字符实际宽度
                    //判断是否自动换行
                    if (this.autoBreak == 1) {
                        //判断是否超出宽度，超出，换行
                        if (left + actulWidth + textCellSpace > w && !TextUtils.isEmpty(result)) {
                            left = 0;
                            top += textHeight;//换行
                            top += this.textRowSpace;//加上行距
                            result += "\n";
                        }
                    }
                    left += actulWidth + textCellSpace;
                    result += vString;//拼接字符串
                }
            }
            left = 0;
        }
        if (this.rate == PutyConst.RATE_180) {//旋转180度的时候换行往上走
            String[] split = result.split("\n");
            if (drawTextList != null && split != null) {
                this.top += textHeight * (drawTextList.length - split.length);//换行
                this.top += this.textRowSpace * (drawTextList.length - split.length);//加上行距
            }
            drawTextList = split;
        } else {
            drawTextList = result.split("\n");
        }
        //得到字体的实际高度
        return top + textHeight;
    }

    // 获取适配的字体下标
    public int getFitFontIndex() {//flag true 是加边框适配字体
        int fitFontIndex;
        if (orientation == 0) {
            fitFontIndex = refitTextHorizontal(content, (int) height);
        } else {
            fitFontIndex = refitTextVertical(content, (int) width);
        }
        return fitFontIndex;
    }

    private int refitTextHorizontal(String textString, int height) {
        String[] lines = content.split("\r\n|\r|\n", -1);
        int availableHeight = height / lines.length;
        int fitFontIndex = 0;
        if (height > 0) {
            TextPaint paint = new TextPaint();
            if (typeface != null) {
                paint.setTypeface(typeface);
            }
            LogUtils.i(TAG, "===============================================>");
//            LogUtils.i(TAG, "DrawAreaYY.dragView.lb.labelHeight:" + DrawArea.dragView.lb.labelHeight);
            LogUtils.i(TAG, "top:" + top);
//            LogUtils.i(TAG, "blankArea:" + UtilYY.getBlankAreaF(lb.printInfo.blankArea) * 2);
            LogUtils.i(TAG, "height:" + height + ":availableHeight:" + availableHeight);
//            LogUtils.i(TAG, "DrawAreaYY.h:" + DrawArea.dragView.h);
            for (int i = 0; i < Global.fontSize.length; i++) {
                float trySize = Global.fontSize[i] * 8 * lb.scale;  //字体不断地增大来适应
                LogUtils.i(TAG, "trySize:" + trySize);
                paint.setTextSize(trySize);
                /*
                * 文字占用高度只跟默认字体有关，UPM=2048
                禁止includePadding时, 实际占用高度为(1900 - -500)/2048 = 117.2%
                开启includePadding时, 实际占用高度为(2174 - -566)/2048 = 133.8%
                * */
                if (paint.descent() - paint.ascent() > availableHeight * 1.1172f) {
                    fitFontIndex = i - 1;
                    // 暂时先注释
//                    if (DrawArea.dragView.lb.fixedLength != 1) {
//                        fitFontIndex = UtilYY.getBGIMGFontIndex(availableHeight, height, fitFontIndex);
//                    }
                    if (fitFontIndex < 0) fitFontIndex = 0;
                    //LogUtils.i(TAG,"fitFontIndex end:"+fitFontIndex);
                    return fitFontIndex;
                }
            }
            return fitFontIndex;
        }
        return fitFontIndex;
    }

    private int refitTextVertical(String text, int textWidth) {
        int fitFontIndex = 0;
        if (textWidth > 0) {
            TextPaint paint = new TextPaint();
            if (typeface != null) {
                paint.setTypeface(typeface);
            }
            for (int i = 0; i < Global.fontSize.length; i++) {
                float trySize = Global.fontSize[i] * 8 * lb.scale;  //字体不断地增大来适应
                paint.setTextSize(trySize);
                float contentWidth = getContentWidthVertical(text, paint);
                if (fontItalic == 1) { // 斜体需要设置右边距，不然文本内容会超显示范围，被截取
                    float cellHeight = paint.descent() - paint.ascent();
                    float tailIndent = 25.f * cellHeight / 180.f;
                    contentWidth += tailIndent;
                }
                if (contentWidth > textWidth) {
                    fitFontIndex = i - 1;
                    // 暂时先注释
//                    if (DrawArea.dragView.lb.fixedLength != 1) {
//                        fitFontIndex = UtilYY.getBGIMGFontIndex(contentWidth, 0, fitFontIndex);
//                    }
                    if (fitFontIndex < 0) fitFontIndex = 0;
                    return fitFontIndex;
                }
            }
            return fitFontIndex;
        }
        return fitFontIndex;
    }


    private float getContentWidthVertical(String textString, TextPaint paint) {
        // 计算总宽度
        String[] lines = content.split("\r\n|\r|\n", -1);
        float contentWidth = 0;
        for (int i = 0; i < lines.length; i++) {
            String item = lines[i];
            float maxLineWidth = 0;
            for (int k = 0; k < item.length(); k++) {
                String s = String.valueOf(item.charAt(k));
                float w = paint.measureText(s);
                if (maxLineWidth < w) {
                    maxLineWidth = w;
                }
            }
            if (maxLineWidth == 0) {
                if (TextUtils.isEmpty(textString)) {
                    maxLineWidth = paint.measureText("一");
                } else {
                    maxLineWidth = paint.measureText(String.valueOf(textString.charAt(0)));
                }
            }
            contentWidth += maxLineWidth;
        }
        return contentWidth;
    }

    private char[] getKeyChar(String str) {
        char[] keys = new char[str.length()];
        for (int i = 0; i < keys.length; i++) {
            keys[i] = str.charAt(i);
        }
        return keys;
    }

    @Override
    public void zoom(float x, float y, float width, float height) {
        super.zoom(x, y, width, height);
        switch (rate) {
            case PutyConst.RATE_0:
                this.width += x;
                if (this.width <= PutyConst.RATE_BOUNDARY) {
                    this.width = PutyConst.RATE_BOUNDARY;
                } else if (this.left + this.width >= width) {
                    this.width -= x;
                    return;
                }
                break;
            case PutyConst.RATE_180:
                this.width -= x;
                this.left += x;
                if (this.width <= PutyConst.RATE_BOUNDARY) {
                    this.width = PutyConst.RATE_BOUNDARY;
                } else if (this.left + this.width >= width) {
                    this.width -= x;
                    return;
                }
                break;
            case PutyConst.RATE_90:
                this.height += y;
                if (this.height <= PutyConst.RATE_BOUNDARY) {
                    this.height = PutyConst.RATE_BOUNDARY;
                } else if (this.top + this.height >= height) {
                    this.height -= y;
                    return;
                }
                break;
            case PutyConst.RATE_270:
                this.height -= y;
                this.top += y;
                if (height <= PutyConst.RATE_BOUNDARY) {
                    this.height = PutyConst.RATE_BOUNDARY;
                } else if (this.top + this.height >= height) {
                    this.height -= y;
                    return;
                }
                break;
        }

        this.rate(this.rate);
        zoomOtherSelected(x, y);
        //重新初始化
        this.initBitmap();
    }

    @Override
    public void rate(int degree) {
        super.rate(degree);
        this.img = this.tempImg;
        if (this.img == null || this.img.isRecycled()) {
            return;
        }
        Matrix matrix = new Matrix();
        matrix.setRotate(degree, this.img.getWidth() / 2, this.img.getHeight() / 2);
        this.img = Bitmap.createBitmap(this.img, 0, 0, this.img.getWidth(), this.img.getHeight(),
                matrix, true);
        rotation(degree);
    }

    public TimeElement clone(Element element,TimeElement timeElement) {
        TimeElement mTimeElement = (TimeElement) element;
        timeElement.textCellSpace = element.textCellSpace;//字间距
        timeElement.dataSourceColIndex = element.dataSourceColIndex;//数据列号
        timeElement.dataSourceColName = element.dataSourceColName;//数据列名
        timeElement.textMode = element.textMode;//居中 靠左 靠右 拉伸
        timeElement.top = element.top; // 元素顶部距离
        timeElement.left = element.left; // 元素左侧距离
        timeElement.scale = element.scale; // 缩放比例
        timeElement.autoBreak = mTimeElement.autoBreak;// 自动换行
        timeElement.textRowMode = mTimeElement.textRowMode;// 行模式
        timeElement.textRowSpace = mTimeElement.textRowSpace;// 行间距
        timeElement.familyName = element.familyName;// 字体名称
        timeElement.typeface = element.typeface; // 字体类型
        timeElement.fontBlod = element.fontBlod; // 加粗
        timeElement.fontDelete = element.fontDelete; // 删除线
        timeElement.fontIndex = element.fontIndex; // 字体大小
        timeElement.fontUnderline = element.fontUnderline; // 下划线
        timeElement.rate = element.rate; // 旋转角度
        timeElement.fontItalic = element.fontItalic; // 斜体 一维码 。文本
        timeElement.isPrinter = element.isPrinter;  // 0 不是打印对象 1打印
        timeElement.ddStep = element.ddStep; // 递变数据 增长步长
        timeElement.date_format = element.date_format; // 日期格式选择项
        timeElement.timedeviation = element.timedeviation; // 日期偏移量（以前用的   这个可能是日期偏移）
        timeElement.datedeviation = element.datedeviation; // 日期偏移量
        timeElement.time_format = element.time_format; // 日期格式选择项
        if (SERIES_TYPE == INDUSTRY_SERIES) {
            timeElement.type = element.type; // 元素类型
            timeElement.isblack = element.isblack; // 黑白显示
            timeElement.isMirror = element.isMirror; // 镜像
        }
        if (SERIES_TYPE == HOUSEHOLD_SERIES) {
            timeElement.width = element.width; // 元素宽
            timeElement.height = element.height; // 元素高
        }
        return timeElement;
    }
}
